/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.core.propertymapper;

import com.inspur.edp.bff.designtime.generator.core.VmCoreGenContext;
import com.inspur.edp.bff.spi.AbstractEntityPropertyMapper;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.item.ClassGenerator;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.formserver.viewmodel.GspViewObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

public class EntityPropertyMapperGenerator extends ClassGenerator {

  private VmCoreGenContext coreGenContext;
  private GspViewObject node;
  private PropertyMappersContext parentContext;

  //Root
  public EntityPropertyMapperGenerator(VmCoreGenContext coreGenContext) {
    this(coreGenContext.getVmGenContext().getVm().getMainObject(), coreGenContext);
  }

  //Child
  public EntityPropertyMapperGenerator(GspViewObject node, VmCoreGenContext coreGenContext,
      PropertyMappersContext parentContext) {
    this(node, coreGenContext);
    this.parentContext = parentContext;
  }

  private EntityPropertyMapperGenerator(GspViewObject node, VmCoreGenContext coreGenContext) {
    super(coreGenContext.getCompilationUnitInfo());
    this.coreGenContext = coreGenContext;
    this.node = node;
  }

  @Override
  protected PropertyMappersContext getContext() {
    return (PropertyMappersContext) super.getContext();
  }

  @Override
  protected ClassGeneratorContext createClassInfoContext() {
    return parentContext == null ? new PropertyMappersContext()
        : new PropertyMappersContext(parentContext);
  }

  @Override
  protected void doInitialize() {
    super.doInitialize();
    if (parentContext != null) {
      parentContext.addMapperTypes(node.getCode(), new MediateType(null, getName()));
    }
    if (node.isRootNode()) {
      coreGenContext.setRootEntityPropMapperType(new MediateType(null, getName()));
    }
  }

  @Override
  protected String getName() {
    return node.getCode().concat("PropertyMapper");
  }

  @Override
  protected ArrayList<ModifierKeyword> getAccessModifier() {
    return new ArrayList<ModifierKeyword>(Arrays.asList(ModifierKeyword.PUBLIC_KEYWORD));
  }

  @Override
  protected ArrayList<ClassMethodGenerator> createMethodGenerators() {
    ArrayList<ClassMethodGenerator> rez = new ArrayList();
    rez.add(new GetBECodeMethodGenerator(coreGenContext, node));
    rez.add(new GetVOCodeMethodGenerator(node));
    rez.add(new GetBEPropNameMethodGenerator(coreGenContext, node));
    rez.add(new GetMapperByVOMethodGenerator(getContext()));
    return rez;
  }

  @Override
  protected TypeInfo setParentClass() {
    return new TypeInfo(AbstractEntityPropertyMapper.class);
  }

  @Override
  protected ArrayList<BaseGenerator> createClassExtendChildGenrators() {
    return new ArrayList(node.getContainChildObjects().stream().filter(
        item -> !item.getIsVirtual() && ((GspViewObject) item).getMapping().getTargetMetadataId()
            .equals(coreGenContext.getVmGenContext()
                .getRefBizEntity().getID()))
        .map(item -> new EntityPropertyMapperGenerator(
            (GspViewObject) item, coreGenContext, getContext()))
        .collect(Collectors.toList()));
  }
}
